//
//  LXBaseVC.m
//  ProjectSource_Demo
//
//  Created by Liu on 15/12/18.
//  Copyright © 2015年 AngryBear. All rights reserved.
//

#import "LXBaseVC.h"


@interface LXBaseVC ()

@property (nonatomic, strong) LXBaseViewModel *viewModel;

@end

@implementation LXBaseVC

- (instancetype)initWithViewModel:(id)viewModel
{
    if (self = [super init]) {
        self.viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.viewModel.navigationController = self.navigationController;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    for (UIView *view in self.view.subviews) {
        [view removeFromSuperview];
    }
}


@end

@implementation UINavigationController (LXPushedViewModel)

- (void)pushViewModel:(LXBaseViewModel *)viewModel animated:(BOOL)animated
{
    NSString *classString = [viewModel.descriptionOfViewModel stringByAppendingString:@"ViewController"];
    Class class = NSClassFromString(classString);
    if (!class) {
        classString = [viewModel.descriptionOfViewModel stringByAppendingString:@"VC"];
        class = NSClassFromString(classString);
    }
    if (class) {
        UIViewController *vc = [[class alloc] init];
        [self.navigationController pushViewController:vc animated:animated];
    }
}

@end

@implementation UIViewController (LXPresentViewModel)

- (void)presentViewModel:(LXBaseViewModel *)viewModel animated:(BOOL)animated completion:(void (^)(void))completion
{
    NSString *classString = [viewModel.descriptionOfViewModel stringByAppendingString:@"ViewController"];
    Class class = NSClassFromString(classString);
    if (!class) {
        classString = [viewModel.descriptionOfViewModel stringByAppendingString:@"VC"];
        class = NSClassFromString(classString);
    }
    if (class) {
        UIViewController *vc = [[class alloc] init];
        [self presentViewController:vc animated:animated completion:completion];
    }
}

@end